﻿using System.Linq;

namespace ViewServices.Security
{
    public class CustomRoleProvider : System.Web.Security.RoleProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);

            ApplicationName = config["applicationName"];
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                return domainModel.Users.Single(m => m.Name == username).Roles.Any(r => r.Name == roleName);
            }
        }

        public override string[] GetRolesForUser(string username)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                return domainModel.Users.Single(u => u.Name == username).Roles.Select(r => r.Name).ToArray();
            }
        }

        public override void CreateRole(string roleName)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                if (domainModel.Roles.Any(r => r.Name == roleName))
                {
                    return;
                }

                var role = domainModel.Roles.Create();
                role.Name = roleName;
                domainModel.SaveChanges();
            }
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                var role = domainModel.Roles.FirstOrDefault(r => r.Name == roleName);

                if (role == null)
                {
                    return false;
                }

                domainModel.Roles.Remove(role);
                domainModel.SaveChanges();

                return true;
            }
        }

        public override bool RoleExists(string roleName)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                return domainModel.Roles.Any(r => r.Name == roleName);
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                var roles = domainModel.Roles.Where(r => roleNames.Any(rn => rn == r.Name)).ToList();
                foreach (var user in domainModel.Users.Where(u => usernames.Any(un => un == u.Name)))
                {
                    foreach (var role in roles)
                    {
                        if (!user.Roles.Contains(role))
                        {
                            user.Roles.Add(role);
                        }
                    }
                }
                domainModel.SaveChanges();
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                var roles = domainModel.Roles.Where(r => roleNames.Any(rn => rn == r.Name)).ToList();
                foreach (var user in domainModel.Users.Where(u => usernames.Any(un => un == u.Name)))
                {
                    foreach (var role in roles)
                    {
                        if (!user.Roles.Contains(role))
                        {
                            user.Roles.Add(role);
                        }
                    }
                }
                domainModel.SaveChanges();
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                return domainModel.Roles.Single(r => r.Name == roleName).Users.Select(u => u.Name).ToArray();
            }
        }

        public override string[] GetAllRoles()
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                return domainModel.Roles.Select(r => r.Name).ToArray();
            }
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            using (var domainModel = new DataAccess.DomainModel())
            {
                return domainModel.Roles.Single(r => r.Name == roleName).Users.Where(u => u.Name.Contains(usernameToMatch)).Select(u => u.Name).ToArray();
            }
        }

        public override string ApplicationName { get; set; }
    }
}